﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace ClientServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Socket socket;
        private void btStart_Click(object sender, EventArgs e)
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Parse(tb1.Text);
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(tb2.Text));
                socket.Connect(point);
                if (socket.IsBound)
                {
                    ShowMsg("连接成功");
                }
                Thread th = new Thread(Receive);
                th.IsBackground = true;
                th.Start();
            }
            catch { }
           
        }
        private void ShowMsg(string str) 
        {
            tbx1.AppendText(str + "\r\n");
        }

        private void btSend_Click(object sender, EventArgs e)
        {
            try
            {
                string str = tbx2.Text.ToString().Trim();

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);

                socket.Send(buffer);
                tbx2.Text = "";
                ShowMsg("我:" + str);
            }
            catch { }
           
        }
        private void Receive() 
        {
            while (true) 
            {
                try 
                {
                    byte[] buffer = new byte[1024 * 1024 * 2];
                    int r = socket.Receive(buffer);
                    if (r == 0) { break; }
                    if(buffer[0]==0)
                    {
                      
                        string str = Encoding.UTF8.GetString(buffer, 1, r-1);
                        ShowMsg(socket.RemoteEndPoint.ToString() + ":" + str);
                    }else if(buffer[0]==1)
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
                        sfd.Title = "请选择保存的文件";
                        sfd.Filter = "所有文件|*.*";
                        sfd.ShowDialog(this);
                        string path = sfd.FileName;
                        using(FileStream fs=new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
                        {
                            fs.Write(buffer,1,r-1);

                        }
                        MessageBox.Show("保存成功");

                    }
                    else if (buffer[0] == 2)
                    {

                    }
                   
                }
                catch { }
               
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 500;i++ )
            {
                this.Location = new Point(200, 180);
                this.Location = new Point(300, 150);
            }
           
        }
    }
}
